/*
 * Based on a haxe mailing list post of Nicolas Cannasse (Tue May 13 19:08:30 CEST 2008)
 * http://lists.motion-twin.com/pipermail/haxe/2008-May/016783.html
 *
 * Yet Another Haxe Library, Copyright (c) 2009, Jean-Philippe CIMAN (Jeanffy)
 * All rights reserved.
 * Permission is hereby granted to use, copy, modify, and distribute this
 * source code, or portions hereof, for any purpose, without fee, subject
 * to the following restrictions:
 *
 * 1. The origin of this source code must not be misrepresented.
 * 2. Altered versions must be plainly marked as such and
 *    must not be misrepresented as being the original source.
 * 3. This copyright notice may not be removed or altered from
 *    any source or altered source distribution.
 *
 * If you use this source code in a product, acknowledgment is not
 * required but would be appreciated.
 *
 * THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
 * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
 * THE PROJECT AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */
/**
	This class is a kind of emulator of the flash8 behaviour for flash9<br/>
	The name of the class is not YahKey to ease porting from flash8 to flash9
**/
#if flash9
import flash.events.Event;
import flash.events.KeyboardEvent;

class Key
{
	public inline static var UP:Int = 38;
	public inline static var DOWN:Int = 40;
	public inline static var LEFT:Int = 37;
	public inline static var RIGHT:Int = 39;
	public inline static var CONTROL:Int = 17;
	public inline static var SPACE:Int = 32;
	public inline static var SHIFT:Int = 16;
	
	private static var stm_Codes:Array<Null<Int>>;
	private static var stm_Time:Int;

	public static function init()
	{
		stm_Codes = new Array<Null<Int>>();
		stm_Time = 0;
		
		flash.Lib.current.stage.addEventListener(KeyboardEvent.KEY_DOWN, callback(onKey, true));
		flash.Lib.current.stage.addEventListener(KeyboardEvent.KEY_UP, callback(onKey, false));
		flash.Lib.current.stage.addEventListener(Event.ENTER_FRAME, onEnterFrame);
	}

	private static function onEnterFrame(event:Event)
	{
		stm_Time++;
	}

	private static function onKey(isDownEvent:Bool, e:KeyboardEvent)
	{
		if (isDownEvent)
			stm_Codes[e.keyCode] = stm_Time;
		else
			stm_Codes[e.keyCode] = null;
	}

	public static function isDown(c:Int)
	{
		return stm_Codes[c] != null;
	}

	public static function isToggled(c:Int)
	{
		return stm_Codes[c] == stm_Time;
	}
}
#else error
#end

